package com.businessportal.web.controllers.calendar;

import com.businessportal.web.common.DateTimeHelper;
import com.businessportal.web.dataaccess.entities.Event;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.EventService;
import com.businessportal.web.dataaccess.services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.SortedSet;

/**
 * Created by Vadim on 7/3/14.
 */

@WebServlet(urlPatterns = "/securepages/calendar_creator.do")
public class CalendarCreatorController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("UTF-8");

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");



            req.setAttribute("events", EventService.INSTANCE.getEventsForUser(user));

        } catch(Exception e){
            req.setAttribute("errors", e.getMessage());
            req.setAttribute("events", new HashSet<Event>());
        }

        req.setAttribute("event_id", 0);
        req.setAttribute("event_title", "");
        req.setAttribute("is_public", true);
        req.setAttribute("notify_me", true);
        req.setAttribute("when_notify_me", "");
        req.setAttribute("start_date", "");
        req.setAttribute("end_date", "");
        req.setAttribute("event_color", "");

        getServletContext().getRequestDispatcher("/securepages/calendar_creator.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            req.setCharacterEncoding("UTF-8");

            Long event_id = Long.parseLong(req.getParameter("event_id"));
            String event_title = req.getParameter("event_title");
            Boolean is_public = Boolean.parseBoolean(req.getParameter("is_public"));
            Boolean notify_me = Boolean.parseBoolean(req.getParameter("notify_me"));
            Date when_notify_me = DateTimeHelper.parseDateFromString(req.getParameter("when_notify_me"));
            Date start_date = DateTimeHelper.parseDateFromString(req.getParameter("start_date"));
            Date end_date = DateTimeHelper.parseDateFromString(req.getParameter("end_date"));
            String event_color = req.getParameter("event_color");
            String event_description = req.getParameter("event_description");

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            if(null != event_id && 0 != event_id){
                EventService.INSTANCE.UpdateEvent(event_id, user, event_title, is_public, start_date,  end_date, when_notify_me, notify_me, event_color, event_description);
            } else {
                Event event = EventService.INSTANCE.save(user, event_title, is_public, when_notify_me, notify_me, start_date, end_date, event_color, event_description);
                user.addEvent(event);
                UserService.INSTANCE.UpdateUser(user);
            }

            req.setAttribute("events", EventService.INSTANCE.getEventsForUser(user));

        }catch(Exception e){
            req.setAttribute("errors", e.getMessage());
            req.setAttribute("events", new HashSet<Event>());
        }

        req.setAttribute("event_id", 0);
        req.setAttribute("event_title", "");
        req.setAttribute("is_public", true);
        req.setAttribute("start_date", "");
        req.setAttribute("end_date", "");
        req.setAttribute("event_color", "");
        req.setAttribute("event_description", "");
        req.setAttribute("notify_me", true);
        req.setAttribute("when_notify_me", "");

        getServletContext().getRequestDispatcher("/securepages/calendar_creator.jsp").forward(req, resp);
    }
}
